#!/opt/local/bin/perl -wd
use strict;
require ChadoConnect;
# use Data::Dumper;
# $Data::Dumper::Sortkeys = 1;

my $dbh = ChadoConnect::make_con($ARGV[0]);
require ChadoPhen;

my $cvterm_lookup = &ChadoPhen::get_cvterm_lookup;
my ($phens, $cvtmerm_phen) = &ChadoPhen::get_phen;

my $test;
### Now do work

 my %old_new =  (
'FBcv:0000147' =>	'FBcv:0000700',	# 'even pair rule expression pattern' =>	'even pair-rule phenotype',
'FBcv:0000148' =>	'FBcv:0000701',	# 'odd pair rule expression pattern' =>  'odd pair-rule phenotype',
'FBcv:0000319' =>	'FBcv:0000703',	# gap expression pattern'	=>	'gap phenotype',
'FBcv:0000323' =>	'FBcv:0000702',	# 'segment polarity expression pattern'=>	'segment polarity phenotype',
'FBcv:0000320' =>	'FBcv:0000734',	# 'terminal expression pattern' => 'terminal phenotype',
'FBcv:0000322' =>	'FBcv:0000699',	# 'pair rule expression pattern' => 'pair rule phenotype'
);

# #### Hmmmmm - did I think this fucking thing out at all?  This is worst data structure possible to query for qualifier !!!!

# # OK - I need to do yet another chado query to find the offending phenotypes!

# my $sth = $dbh->prepare('SELECT p.phenotype_id AS pid from phenotype p, phenotype_cvterm q, cvterm c where p.phenotype_id=q.phenotype_id and q.cvterm_id=c.cvterm_id and c.name in (\'even pair rule expression pattern\', \'odd pair rule expression pattern\', \'gap expression pattern\', \'segment polarity expression pattern\', \'segment polarity expression pattern\', \'pair rule expression pattern\')');  # Ugghhh - you build all these objects and then you fucking hard wire shit?
# $sth->execute or die "WARNING: ERR: Unable to execute query\n";

  
# my %new_phen;
# while (my $hash_ref = $sth->fetchrow_hashref) {
#   my $pid = $hash_ref->{'pid'};
#   my $phen = $phens->{$pid};
#   my @new_q;
#   if ($phen->qlist) {
#     for (@$phen->qlist) {
#       push @new_q, $cvterm_lookup->{$_}, unless (exists $cvterm_lookup->cvterm_obo_id->{$old_new{$_}})
#     }
#   } 
#   my $new_phen1 = Phen->new(primary_term => $phen->primary_term, q_list => \@new_q );
#   my $new_phen2 = Phen->new(primary_term => $cvterm_lookup->cvterm_obo_id->{$old_new{$_}}, q_list => q_list => \@new_q);
#   $new_phen1->roll_uname();
#   $new_phen2->roll_uname();
#   push @{$new_phen{$pid}}, $new_phen1;
#   push @{$new_phen{$pid}}, $new_phen2;
# }
$a = 1;

 print $phens->{'9411'}->uname."\n";
 print $phens->{'9411'}->primary_term->name."\n";
for (@{$phens->cvterm_cid->{'9411'}->q_array}) {
  print $_->name."\n"
}
